<?php
namespace jy_supplier;
defined('BY_JYA') or exit('error');
/*
	配送
*/
class pei extends \plugin{
	public function checkclerk(){
		$sid = pdo_getcolumn('jy_supplier_clerk',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
		if(!$sid){
			exi('你不是仓库员工！','error');
		}
		return $sid;
	}
	public function updayorder(){
		$this->up_allsend();
		$this->up_ordersend();
		return true;
	}
	//清单打印
	public function up_allsend(){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$lastdaytime=$daytime-24*3600;
		$up_hour=$this->setting['up_hour'];
		if($up_hour){
			$nowhour=date('H');
			if($up_hour>intval($nowhour)){
				$endtime=$lastdaytime+$up_hour*3600;
			}else{
				$endtime=$daytime+$up_hour*3600;
			}
			$dayid=date('Y-m-d',$endtime+24*3600);
			$startime=$endtime-24*3600;
		}else{
			$endtime=$daytime;
			$startime=$lastdaytime;
			$dayid=date('Y-m-d');
		}
		$isup=pdo_get('jy_supplier_log',array('uniacid'=>$_SESSION['uniacid'],'dayid'=>$dayid));
		if($isup['id']){
			return false;
		}
		pdo_update('jy_supplier_log',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'dayid <>'=>$dayid));
		$params=array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'api_id <>'=>0,'api_pluginname'=>'jy_supplier');
		$params['createtime between']=array($startime,$endtime);
		$orders=pdo_getall('jy_weishop_order_goods',$params);
		$merchorder=array();
		$depot_id=pdo_getcolumn('jy_supplier_depot',array('uniacid'=>$_SESSION['uniacid']),'id');
		foreach($orders as $item){
			$goodskey=$item['goodsid'].'_'.$item['optionid'];
			if(!$merchorder[$item['api_id']]){
				$allnum=$item['total'];
				$merchorder[$item['api_id']]=array(
					'merchid'=>$item['api_id'],
					'depot_id'=>$depot_id,
					'allnum'=>$allnum,
					'goods'=>$goods,
				);
			}else{
				$allnum +=$item['total'];
				$merchorder[$item['api_id']]['allnum']=$allnum;
			}
			if(!$merchorder[$item['api_id']]['goods'][$goodskey]){
				$title=pdo_getcolumn('jy_weishop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),'title');
				//商品规格
				if($item['optionid']){
						$options=pdo_get('jy_weishop_goods_option',array('id'=>$item['optionid']),array('title','specs'));
						if($item['istaocan']){
							$leititle=$options['title'];
						}else{
							$specs=$options['specs'];
							$specs=explode("_",$specs);
							foreach($specs as &$spec){
								$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
							}
							$leititle=implode("，",$specs);
						}
				}else{
						$item['optionid']=0;
				}
				$merchorder[$item['api_id']]['goods'][$goodskey]=array('title'=>$title,'leititle'=>$leititle?:'','optionid'=>$item['optionid'],'goodsid'=>$item['goodsid'],'num'=>$item['total']);
			}else{
				$merchorder[$item['api_id']]['goods'][$goodskey]['num'] +=$item['total'];
			}
		}
		foreach($merchorder as $item){
			$item['goods']=ijson_encode($item['goods']);
			$item['createtime']=$endtime+100;
			$item['uniacid']=$_SESSION['uniacid'];
			$item['dayid']=$dayid;
			$this->pdo_insert('log',$item);
		}
	}
	//按订单打印
	public function up_ordersend(){
		$dayid=date('Y-m-d');
		$startime=time()-7*24*3600;
		$params=array('ispay'=>1,'status'=>1,'uniacid'=>$_SESSION['uniacid'],'api_id <>'=>0,'api_pluginname'=>'jy_supplier');
		$params['createtime >']=$startime;
		$orders=pdo_getall('jy_weishop_order_goods',$params,array('orderid','istaocan','goodsid','optionid','total','api_id','price','total_price'));
		$demodepot_id=pdo_getcolumn('jy_supplier_depot',array('uniacid'=>$_SESSION['uniacid']),'id');
		$demoline_id=pdo_getcolumn('jy_supplier_line',array('uniacid'=>$_SESSION['uniacid'],'depotid'=>$demodepot_id),'id');
		$merchorder=array();
		foreach($orders as $item){
			$isup=pdo_get('jy_supplier_log',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['orderid']));
			if($isup['id']){
				continue;
			}
			$item['num']=$item['total'];
			$order=pdo_get('jy_weishop_order',array('id'=>$item['orderid']),array('address','addressid'));
			//用户地址
			$order['address']=json_decode($order['address'],true);
			if($order['address'] && $order['address']['name']){
				$address=$order['address'];
			}else{
				$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
			}
			if(!is_array($address['pcd'])){
				$address['pcd']=json_decode($address['pcd'],true);
			}
			$depot_id=pdo_getcolumn('jy_supplier_depot',array('uniacid'=>$_SESSION['uniacid'],'province'=>$address['pcd']['province'],'city'=>$address['pcd']['city'],'district'=>$address['pcd']['district']),'id');
			$line_id=pdo_getcolumn('jy_supplier_line',array('uniacid'=>$_SESSION['uniacid'],'depotid'=>$depot_id?:$demodepot_id,'street'=>$address['pcd']['street']),'id');
			if(!$merchorder[$item['orderid']]){
				$merchorder[$item['orderid']]=array();
			}
			$item['title']=pdo_getcolumn('jy_weishop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),'title');
			//商品规格
			if($item['optionid']){
						$options=pdo_get('jy_weishop_goods_option',array('id'=>$item['optionid']),array('title','specs'));
						if($item['istaocan']){
							$item['leititle']=$options['title'];
						}else{
							$specs=$options['specs'];
							$specs=explode("_",$specs);
							foreach($specs as &$spec){
								$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
							}
							$item['leititle']=implode("，",$specs);
						}
			}
			if(!$merchorder[$item['orderid']][$item['api_id']]){
				$goods=array();
				$goods[]=$item;
				$logs=array(
						'merchid'=>$item['api_id'],
						'depot_id'=>$depot_id?:$demodepot_id,
						'line_id'=>$line_id?:$demoline_id,
						'allnum'=>$item['total'],
						'orderid'=>$item['orderid'],
						'goods'=>$goods,
				);
				$merchorder[$item['orderid']][$item['api_id']]=$logs;	
			}else{
				$merchorder[$item['orderid']][$item['api_id']]['allnum'] +=$item['total'];
				$merchorder[$item['orderid']][$item['api_id']]['goods'][]=$item;
			}
		}
		foreach($merchorder as $mo){
			foreach($mo as $mv){
				$logdata=array(
					'goods'=>ijson_encode($mv['goods']),
					'createtime'=>time(),
					'uniacid'=>$_SESSION['uniacid'],
					'orderid'=>$mv['orderid'],
					'dayid'=>$dayid,
					'merchid'=>$mv['merchid'],
					'depot_id'=>$mv['depot_id'],
					'line_id'=>$mv['line_id'],
					'allnum'=>$mv['allnum'],
				);
				$this->pdo_insert('log',$logdata);
			}
		}
		return true;
	}
	public function upprint($logid,$goods=array(),$isdemo=false,$print_pcd=0){
		if(!$print_pcd){
			$log=pdo_get('jy_supplier_log',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
			if(!$log){
				exi('记录不存在！','error');
			}
			$loggoods=json_decode($log['goods'],true);
			foreach($loggoods as $k=>&$item){
				$item['isprintnum']=$item['isprintnum']?:0;
				if($goods[$k]['printnum']>0){
					$item['isprintnum'] +=$goods[$k]['printnum'];
					$item['printnum']=$goods[$k]['printnum'];
					$this->model('printmsg')->goodsprint($item,$log);
				}
			}
			if(!$isdemo){
				pdo_update('jy_supplier_log',array('goods'=>ijson_encode($loggoods)),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
			}
		}else{
			foreach($goods as $k=>&$item){
				if($item['selected']){
					foreach($item['goods'] as $kg=>&$gv){
						$gv['isprintnum']=$gv['isprintnum']?:0;
						$lastprintnum=$gv['num']-$gv['isprintnum'];
						if($lastprintnum>0){
							$gv['isprintnum'] +=$lastprintnum;
							$gv['printnum']=$lastprintnum;
							$this->model('printmsg')->goodsprint($gv,$item);
						}
					}
					pdo_update('jy_supplier_log',array('goods'=>ijson_encode($item['goods'])),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}
		return true;
	}
	public function self_verify($logid){
		$log=pdo_get('jy_supplier_log',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
		if(!$log){
			exi('记录不存在！','error');
		}
		pdo_update('jy_supplier_log',array('status'=>1),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
}